<?php echo $this->element('paginator_ajax_settings'); ?>
<h1>Xem tất cả khách sạn</h1>

<div id="filter">
    <div class="search">
        Địa điểm:
        <?php echo $this->Form->input('Hotel.location_id', array('type' => 'select', 'options' => $locations, 'class' => 'dropdown', 'label' => false, 'div' => false)); ?>
        &nbsp;&nbsp;&nbsp;Tìm kiếm:
        <?php echo $this->Form->input('Hotel.query', array('type' => 'text', 'value' => isset($query) ? htmlspecialchars_decode($query) : '', 'class' => 'textfield', 'label' => false, 'div' => false)); ?></span>
    </div>

    <br class="clr" />
</div>

<script type="text/javascript">
$(document).ready(function (){
    var timerid;
    <?php if (isset($query)): ?>
    $('#HotelQuery').focus();
    <?php endif; ?>
    $('#HotelQuery').keyup(function (e){
        if (e.keyCode == 13)
        {
            search($('#HotelQuery').val(), $('#HotelMonth').val(), $('#HotelYear').val());
        }
    });
    $('#HotelQuery').click(function (e){
        $('#HotelQuery').select();
    });
    $('#HotelLocationId').change(function (e){
        search($('#HotelQuery').val(), $('#HotelLocationId').val());
    });
    function search(query, location_id)
    {
        if (location_id == 'undefined' || location_id == null)
        {
            location_id = $('#HotelMonth').val();
        }
        //query = $('#CustomerQuery').val();
        clearTimeout(timerid);
        timerid = setTimeout(function (){
            $.ajax({
                async: true,
                type: "POST",
                url: "hotels/index/query:" + query + "/location_id:" + location_id,
                //data: "",
                success: function(msg){
                    $('#main').html(msg);
                },
                beforeSend: function (XMLHttpRequest){
                    $("#busy-indicator").fadeIn();
                },
                complete: function (XMLHttpRequest, textStatus){
                    $("#busy-indicator").fadeOut();
                }
            });
        }, 600);
    }
    var availableTags = [
        <?php
        if (!empty($all_hotels))
        {
            $count = 0;
            $hotels_count = sizeof($all_hotels);
            foreach ($all_hotels as $hotel)
            {
                echo "\"" . $hotel['Hotel']['name'] . "\"" . ($count < $hotels_count ? "," : "");
                $count++;
            }
        }
        ?>
    ];
    $( "#HotelQuery" ).autocomplete({
        source: availableTags,
        select: function (event, ui){
            search(ui.item.value.trim());
        }
    });
});
</script>

<table class="table1">
    <tr>
        <th><?php echo $this->Paginator->sort('Khách sạn', 'name'); ?></th>
        <th><?php echo $this->Paginator->sort('Chất lượng', 'rating'); ?></th>
        <th><?php echo $this->Paginator->sort('Địa điểm', 'Location.name'); ?></th>
        <th></th>
    </tr>
    <?php foreach ($hotels as $hotel): ?>
    <tr>
        <td><?php echo $this->Html->link($hotel['Hotel']['name'], array('controller' => 'hotels', 'action' => 'edit', $hotel['Hotel']['id'])); ?></td>
        <td><?php echo $hotel['Hotel']['rating'] ? $hotel['Hotel']['rating'] . ' sao' : 'Không có đánh giá' ?></td>
        <td><?php echo $hotel['Location']['name'] ?></td>
        <td>
            <?php echo $this->Html->link('Xóa', array('controller' => 'hotels', 'action' => 'delete', $hotel['Hotel']['id']), array(), "Bạn có chắc xóa khách sạn này?"); ?>
        </td>
    </tr>
    <?php endforeach; ?>
</table>

<?php echo $this->element('paginator'); ?>